package com.zzh.controller;

import com.zzh.domain.Course;
import com.zzh.domain.CourseLesson;
import com.zzh.domain.CourseSection;
import com.zzh.domain.ResponseResult;
import com.zzh.service.CourseContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("courseContent")
public class CourseContentController {

    @Autowired
    private CourseContentService courseContentService;

    @RequestMapping("/findSectionAndLesson")
    public ResponseResult findSectionAndLesson(@RequestParam int courseId) {
        List<CourseSection> courseSections = courseContentService.findSectionAndLesson(courseId);
        ResponseResult result = new ResponseResult(true, 200, "成功响应", courseSections);
        return result;
    }

    @RequestMapping("/findCourseByCourseId")
    public ResponseResult findCourseByCourseId(@RequestParam int courseId) {
        Course course = courseContentService.findCourseByCourseId(courseId);
        Map<String, Object> map = new HashMap<>();
        map.put("id", course.getId());
        map.put("courseName", course.getCourseName());
        ResponseResult result = new ResponseResult(true, 200, "成功响应", map);
        return result;
    }

    @RequestMapping("/saveOrUpdateSection")
    public ResponseResult saveOrUpdateSection(@RequestBody CourseSection section) {
        if (section.getId() == null) {
            courseContentService.saveSection(section);
            return new ResponseResult(true, 200, "成功响应", null);
        } else {
            courseContentService.updateSection(section);
            return new ResponseResult(true, 200, "成功响应", null);
        }
    }

    @RequestMapping("/updateSectionStatus")
    public ResponseResult updateSectionStatus(@RequestParam int id, @RequestParam int status) {
        courseContentService.updateSectionStatus(id, status);
        Map<String, Integer> map = new HashMap<>();
        map.put("status", status);
        return new ResponseResult(true, 200, "成功响应", map);
    }

    @RequestMapping("/saveOrUpdateLesson")
    public ResponseResult saveOrUpdateLesson(@RequestBody CourseLesson lesson) {
        if (lesson.getId() == null) {
            courseContentService.saveLesson(lesson);
            return new ResponseResult(true, 200, "成功响应", null);
        }else {
            courseContentService.updateLesson(lesson);
            return new ResponseResult(true, 200, "成功响应", null);
        }
    }
}
